package com.zzl.bonds.ticket.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzl.bonds.ticket.dao.ContractTemplateDao;
import com.zzl.bonds.ticket.dto.ContractemplateAddDto;
import com.zzl.bonds.ticket.dto.ContractemplateUpdateDto;
import com.zzl.bonds.ticket.entity.ContractTemplateEntity;
import com.zzl.bonds.ticket.service.ContractemplateService;
import com.zzl.general.common.exception.SysException;
import com.zzl.general.common.serurity.entity.User;
import com.zzl.general.common.utils.PageUtils;
import com.zzl.general.common.utils.Query;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * 合同模板
 *
 * @author sunpp
 * @date 2022-02-16 14:04:04
 */
@Service("contractemplateService")
public class ContractemplateServiceImpl extends ServiceImpl<ContractTemplateDao, ContractTemplateEntity> implements ContractemplateService {

    /**
    * 分页查询
    *
    * @param params 查询参数
    * @return 分页数据
    */
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<ContractTemplateEntity> page = this.page(
                new Query<ContractTemplateEntity>().getPage(params),
                new QueryWrapper<>()
        );

        return new PageUtils(page);
    }

    /**
     * 新增
     *
     * @param contractemplateAddDto 新增实体
     * @param user 当前登录用户
     * @throws SysException 业务异常
     */
    @Override
    public void saveContractemplate(ContractemplateAddDto contractemplateAddDto, User user) throws SysException {
        ContractTemplateEntity contractemplateEntity = new ContractTemplateEntity();
        BeanUtils.copyProperties(contractemplateAddDto, contractemplateEntity);
        contractemplateEntity.setCreatorId(user.getId());
        if(!this.save(contractemplateEntity)){
            throw new SysException("A00001", "保存失败！");
        }
    }

    /**
     * 修改
     *
     * @param contractemplateUpdateDto 修改实体
     * @param user 当前登录用户
     * @throws SysException 业务异常
     */
    @Override
    public void updateContractemplateById(ContractemplateUpdateDto contractemplateUpdateDto, User user) throws SysException {
        ContractTemplateEntity contractemplateEntity = new ContractTemplateEntity();
        BeanUtils.copyProperties(contractemplateUpdateDto, contractemplateEntity);
        contractemplateEntity.setUpdaterId(user.getId());
        if(!this.updateById(contractemplateEntity)){
            throw new SysException("A00002", "修改失败！");
        }
    }

}